﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ChiTietBanSachDAO
    {
        public int Insert(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            DTO.ChiTietBanSachDTO dto)
        {
            int r = -1;
            try
            {
                List<System.Data.SqlClient.SqlParameter> prs =
                   new List<System.Data.SqlClient.SqlParameter>();
                prs.Add(new System.Data.SqlClient.SqlParameter("@IDSach", dto.IDSach));
                prs.Add(new System.Data.SqlClient.SqlParameter("@IDHoaDon", dto.IDHoaDon));
                prs.Add(new System.Data.SqlClient.SqlParameter("@SoLuong", dto.SoLuong));
                prs.Add(new System.Data.SqlClient.SqlParameter("@DonGia", dto.DonGia));
                if (connection == null || transaction == null)
                    r = DAL.sqlHelper.ExecuteNoneQuery("ChiTietBanSach_Insert", prs);
                else
                    r = DAL.sqlHelper.ExecuteNoneQuery(connection, transaction, "ChiTietBanSach_Insert", prs);
                return r;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("Insert failed " + ex.Message);
            }
            finally
            {
            }
        }

        public List<DTO.ChiTietBanSachDTO> SelectByHoaDon(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            int IDHoaDon)
        {
            List<DTO.ChiTietBanSachDTO> r = new List<DTO.ChiTietBanSachDTO>();
            try
            {
                List<System.Data.SqlClient.SqlParameter> prs =
                    new List<System.Data.SqlClient.SqlParameter>();
                prs.Add(new System.Data.SqlClient.SqlParameter("@IDHoaDon", IDHoaDon));
                System.Data.DataTable dt = null;
                if (connection == null || transaction == null)
                    dt = DAL.sqlHelper.ExecuteQuery("ChiTietBanSach_SelectAllByHoaDon", prs);
                else
                    dt = DAL.sqlHelper.ExecuteQuery(connection, transaction,
                        "ChiTietBanSach_SelectAllByHoaDon", prs);
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    DTO.ChiTietBanSachDTO dto = new DTO.ChiTietBanSachDTO();
                    dto.IDSach = int.Parse(dr["IDSach"].ToString());
                    dto.IDHoaDon = int.Parse(dr["IDHoaDon"].ToString());
                    dto.SoLuong = int.Parse(dr["SoLuong"].ToString());
                    dto.DonGia = float.Parse(dr["DonGia"].ToString());
                    r.Add(dto);
                }
                return r;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("SelectByHoaDon failed " + ex.Message);
            }
            finally
            {
            }
        }
    }
}
